using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Data.SqlServerCe;

namespace FeedFly
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void parseFile(object sender, EventArgs e)
        {
            XmlReaderSettings rdrSettings = new XmlReaderSettings();
            rdrSettings.IgnoreWhitespace = true;
            rdrSettings.IgnoreComments = true;
            rdrSettings.ValidationType = ValidationType.None;

            string fileName = @"\Program Files\FeedFly\yahoosportsrss.xml";

            FileStream rssFileStream = new FileStream(fileName,FileMode.Open);

            StreamReader reader = new StreamReader(rssFileStream);

            int startParse = Environment.TickCount;

            Feed feed = Feed.GetFeed(1, "http://rss.news.yahoo.com/rss/elections", 25);

            int finishParse = Environment.TickCount;

            rssFileStream.Close();

            MessageBox.Show(finishParse - startParse + " ms to parse");

            int startPersist = Environment.TickCount;

            feed.Persist();

            int finishPersist = Environment.TickCount;

            MessageBox.Show(finishPersist - startPersist + " ms to persist");

            // read data back from database

            SqlCeDataReader itemRdr = Data.ExecuteReader("SELECT * FROM FeedItem WHERE FeedId = " + feed.Id);

            string itemTitle = null;
            int itemCount = 0;

            while (itemRdr.Read())
            {
                itemTitle = itemRdr.GetString(itemRdr.GetOrdinal("Title"));
                itemCount++;
            }

            MessageBox.Show("Item count in database: " + itemCount.ToString());

        }

        private void DownloadDilbertUpdates(object sender, EventArgs e)
        {
            // download update to Dilbert feed
            Feed feed = Feed.GetFeed(1);
            feed.DownloadUpdate();
            feed.Persist();           
        }

        private void newFeedButton_Click(object sender, EventArgs e)
        {

            int startDL = Environment.TickCount;

             Feed feed = Feed.GetFeed(1, "http://energylegalblog.com/rss.aspx", 100);

            int endDL = Environment.TickCount;

            MessageBox.Show(endDL - startDL + " ms to download and parse");

            int startPersist = Environment.TickCount;

            feed.Persist();

            int finishPersist = Environment.TickCount;

            MessageBox.Show(finishPersist - startPersist + " ms to persist to database");

            // read data back from database
            MessageBox.Show("Item count in database: " + Data.ExecuteScalar("SELECT COUNT(*) FROM FeedItem WHERE FeedId = " + feed.Id));
        }

        private void TestForm_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyCode.ToString());

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("2");
        }


    }
}